package com.crm.business.crm.log;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Dict;
import com.crm.business.admin.service.AdminService;
import com.crm.business.crm.service.ICrmReFundService;
import com.crm.common.crm.constant.CrmEnum;
import com.crm.common.crm.entity.BO.CrmBusinessChangOwnerUserBO;
import com.crm.common.crm.entity.BO.CrmContractSaveBO;
import com.crm.common.crm.entity.BO.CrmUpdateInformationBO;
import com.crm.common.crm.entity.PO.CrmRefund;
import com.crm.common.ka.core.common.log.Content;
import com.crm.business.servlet.ApplicationContextHolder;
import com.crm.common.utils.spring.SpringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class CrmRefundLog {


    private crmSysLogUtil sysLogUtil = SpringUtils.getBean(crmSysLogUtil.class);

    private ICrmReFundService crmReFundService = SpringUtils.getBean(ICrmReFundService.class);

    private AdminService adminService = SpringUtils.getBean(AdminService.class);

    public Content update(CrmContractSaveBO crmModel) {
        CrmRefund crmRefund = BeanUtil.copyProperties(crmModel.getEntity(), CrmRefund.class);
        sysLogUtil.updateRecord(crmModel.getField(), Dict.create().set("batchId", crmRefund.getBatchId()).set("dataTableName", "bill_crm_contract_data"));
        CrmRefund refund = crmReFundService.getById(crmRefund.getRefundId());
        return sysLogUtil.updateRecord(BeanUtil.beanToMap(refund), BeanUtil.beanToMap(crmRefund), CrmEnum.REFUND, crmRefund.getNumber());
    }

    public List<Content> deleteByIds(List<Integer> ids) {
        List<Content> contentList = new ArrayList<>();
        for (Integer id : ids) {
            CrmRefund crmRefund = crmReFundService.getById(id);
            if (crmRefund != null) {
                contentList.add(sysLogUtil.addDeleteActionRecord(CrmEnum.REFUND, crmRefund.getNumber()));
            }
        }
        return contentList;
    }

    public List<Content> changeOwnerUser(CrmBusinessChangOwnerUserBO crmChangeOwnerUserBO) {
        return crmChangeOwnerUserBO.getIds().stream().map(id -> {
            CrmRefund refund = crmReFundService.getById(id);
            return sysLogUtil.addConversionRecord(CrmEnum.REFUND, crmChangeOwnerUserBO.getOwnerUserId(), refund.getNumber());
        }).collect(Collectors.toList());
    }

    public List<Content> updateInformation(CrmUpdateInformationBO updateInformationBO) {
        List<Content> contentList = new ArrayList<>();
        String batchId = updateInformationBO.getBatchId();
        updateInformationBO.getList().forEach(record -> {
            CrmRefund oldRefund = crmReFundService.getById(updateInformationBO.getId());
            Map<String, Object> oldRefundMap = BeanUtil.beanToMap(oldRefund);
            if (record.getInteger("fieldType") == 1) {
                Map<String, Object> crmRefundMap = new HashMap<>(oldRefundMap);
                crmRefundMap.put(record.getString("fieldName"), record.get("value"));
                CrmRefund crmRefund = BeanUtil.mapToBean(crmRefundMap, CrmRefund.class, true);
                contentList.add(sysLogUtil.updateRecord(oldRefundMap, crmRefundMap, CrmEnum.REFUND, crmRefund.getNumber()));
            }
        });
        return contentList;
    }
}
